package ipdesk.model.analysis.action;

import ipdesk.model.analysis.bean.AnalysisChannelBean;
import ipdesk.model.analysis.dao.AnalysisChannelDao;
import ipdesk.model.analysis.dao.AnalysisSeminarDao;


import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.Time;

public class AnalysisChannelAction extends ActionSupport implements Preparable, ModelDriven<AnalysisChannelBean>
{
	private AnalysisChannelBean analysisChannelBean;
	private List<AnalysisChannelBean> yearList;
	private List<AnalysisChannelBean> analysisChannelList;
	private List<AnalysisChannelBean> analysisChannelListByMonth;
	
	
	

	public String execute() throws Exception
	{
		
		// 1. 년도 셀렉트 박스 리스트를 가져온다.
		yearList = AnalysisChannelDao.getInstance().getYears();
		
		
		// 2. 조회에 필요한 년도 파라메터가 없다면 올해로 검색한다.
		String selectedYear = "";
		
		if(analysisChannelBean.getSelectedYear() == null || "".equals(analysisChannelBean.getSelectedYear())){
			selectedYear = Time.getYear();
			analysisChannelBean.setSelectedYear(selectedYear);
		}else{
			selectedYear = analysisChannelBean.getSelectedYear();
		}
		
		analysisChannelList = AnalysisChannelDao.getInstance().getAnalysisChannelList(selectedYear);
		//System.out.println("analysisChannelList.size() ::: "+analysisChannelList.size());
		
		return SUCCESS;
	}

	public String executeGetDaysResults() throws Exception
	{
		// 1. 페이지에서 가져온, 년도, 월에 따른 마지막 일자를 구한다.
		String selectedYear = "";
		String month		= "";
		int days			= 0;
		
		if(analysisChannelBean.getSelectedYear() == null || "".equals(analysisChannelBean.getSelectedYear())){
			selectedYear = Time.getYear();
			analysisChannelBean.setSelectedYear(selectedYear);
		}else{
			selectedYear = analysisChannelBean.getSelectedYear();
		}
		//System.out.println("selectedYear : "+selectedYear);
		if(analysisChannelBean.getMonth() == null || "".equals(analysisChannelBean.getMonth())){
			month = "01";
			analysisChannelBean.setMonth(month);
		}else{
			month = analysisChannelBean.getMonth();
		}
		//System.out.println("month : "+month);
		
		days = Time.getLastDay(selectedYear, month);
		analysisChannelBean.setDays(String.valueOf(days));
		//System.out.println("days : "+days);
		
		// 2. 쿼리 요청
		
		analysisChannelListByMonth = AnalysisChannelDao.getInstance().getAnalysisChannelListByMonth(analysisChannelBean);
		//System.out.println("analysisSeminarListByMonth.size :: "+analysisSeminarListByMonth.size());
		return SUCCESS;
	}
	
	public void prepare() throws Exception {
		analysisChannelBean = new AnalysisChannelBean();
		
	}

	public AnalysisChannelBean getModel() {

		return analysisChannelBean;
	}

	public List<AnalysisChannelBean> getYearList() {
		return yearList;
	}

	public List<AnalysisChannelBean> getAnalysisChannelList() {
		return analysisChannelList;
	}
	
	public List<AnalysisChannelBean> getAnalysisChannelListByMonth() {
		return analysisChannelListByMonth;
	}
}

